library UnitEvent initializer Init requires TriggerUtils

    public function interface Response takes unit u returns nothing

    public function interface TowResponse takes unit us, unit ut returns nothing

    globals
        private Response array callBacks1
        private integer index1 = 0

        private TowResponse array callBacks2
        private integer index2 = 0
    endglobals

    // ============================[API]====================================
    function RegisterUnitDeathResponse takes Response r returns nothing
        set callBacks1[index1] = r
        set index1 = index1 + 1
    endfunction

    function RegisterUnitSummonResponse takes TowResponse r returns nothing
        set callBacks2[index2] = r
        set index2 = index2 + 1
    endfunction
    // ================================================================

    private function onUnitDeath takes nothing returns nothing
        local integer i
        set i = 0
        loop
            exitwhen i >= index1
            call callBacks1[i].evaluate(GetTriggerUnit())
            set i = i + 1
        endloop
    endfunction

    private function onUnitSummon takes nothing returns nothing
        local integer i
        set i = 0
        loop
            exitwhen i >= index2
            call callBacks2[i].evaluate(GetSummoningUnit(), GetSummonedUnit())
            set i = i + 1
        endloop
    endfunction

    private function InitTrigger takes playerunitevent evt, code c returns nothing
        local trigger tr = CreateTrigger()
        call TriggerRegisterAllPlayerEvent(tr, evt, c)
        set tr = null
    endfunction

    private function Init takes nothing returns nothing
        call InitTrigger(EVENT_PLAYER_UNIT_DEATH, function onUnitDeath)
        call InitTrigger(EVENT_PLAYER_UNIT_SUMMON, function onUnitSummon)
    endfunction

endlibrary
